package com.cskaoyan.market.service.wx.impl;

import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.db.domain.MarketGoodsExample;
import com.cskaoyan.market.db.domain.MarketTopic;
import com.cskaoyan.market.db.domain.MarketTopicExample;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketTopicMapper;
import com.cskaoyan.market.service.wx.WxTopicService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName: TopicServiceImpl
 * @Auther: lyz
 * @Date: 2023/05/20/22:39
 * @Description:
 */
@Service
public class WxTopicServiceImpl implements WxTopicService {
    @Autowired
    MarketTopicMapper marketTopicMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    private static final Integer RELATED_LIMIT = 4;

    @Override
    public List<MarketTopic> list(Integer page, Integer limit) {
        MarketTopicExample topicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = topicExample.or();
        criteria.andDeletedEqualTo(false);
        PageHelper.startPage(page, limit);
        List<MarketTopic> marketTopicList = marketTopicMapper.selectByExample(topicExample);
        return marketTopicList;
    }

    //TODO 取出goodsID


    @Override
    public List<MarketTopic> related(Integer id) {
        MarketTopicExample topicExample = new MarketTopicExample();
        topicExample.or().andDeletedEqualTo(false);
        PageHelper.startPage(1, RELATED_LIMIT);
        List<MarketTopic> topicList = marketTopicMapper.selectByExample(topicExample);
        if (topicList.size() > RELATED_LIMIT) {
            topicList = topicList.subList(0, RELATED_LIMIT);
        }
        return topicList;
    }

//    @Override
//    public HashMap<String, Object> related(Integer id) {
//        //根据传入id查找附近4个相关的id;
//        MarketTopicExample topicExample = new MarketTopicExample();
//        topicExample.or().andDeletedEqualTo(false);
//        PageHelper.startPage(1, RELATED_LIMIT);
//        List<MarketTopic> topicList = marketTopicMapper.selectByExample(topicExample);
//        if (topicList.size() > RELATED_LIMIT) {
//            topicList = topicList.subList(0, RELATED_LIMIT);
//        }
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("limit", RELATED_LIMIT);
//        map.put("list", VoUtil.objectToVolist(topicList, TopicRelatedVo.class));
//        map.put("page", 1);
//        map.put("pages", topicList.size() / RELATED_LIMIT);
//        map.put("total", topicList.size());
//        return map;
//    }
//        //List<HashMap<String, Object>> list = new ArrayList<>();
//        //list.add(map);
//       // return list;


        @Override
        public MarketTopic detailTopic (Integer id){
            MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);
            return marketTopic;
        }

        @Override
        public List<MarketGoods> detailGoods (Integer id){
            MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);
            Integer[] goods = marketTopic.getGoods();
            MarketGoodsExample goodsExample = new MarketGoodsExample();
            goodsExample.or().andCategoryIdEqualTo(id);
            List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(goodsExample);
            return marketGoods;
        }
    }





